package com.ys.controller.web.purchase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.purchase.TcgInvoiceRegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.purchase.TcgInvoiceRegist;
import com.ys.entity.purchase.TcgInvoiceRegistItem;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/purchase/tcgInvoiceRegist")
public class WebTcgInvoiceRegistController {

    @Autowired
    private CommonApiService<TcgInvoiceRegist> commonApiService;

	@Autowired
	private TcgInvoiceRegistService tcgInvoiceRegistService;
    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> sonlist = new ArrayList<>();

        // 子列表配置信息
        SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCG_InvoiceRegist, sonlist);
        try {
            String bid = commonApiService.getCommonNextBid(MenuId.TCG_InvoiceRegist);

            TcgInvoiceRegist entity = new TcgInvoiceRegist()
                    .setBid(bid)
                    .setCreateTime(new Date());
            Map<String, Object> extraParam = new HashMap<String, Object>();

            String aa =  commonApiService.toAdd(model, entity, MenuId.TCG_InvoiceRegist, extraParam, subGridConfig);
            
            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
            	return "purchase/tcgInvoiceRegist/toAdd";
			} else {
				return aa;
			}
            

        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toFlow")
    public String toFlow(Model model, HttpServletRequest request, String bid) {
        try {
            if (bid == null) {
                return W.error(model, "单号不能为空");
            }

            TcgInvoiceRegist entity = new TcgInvoiceRegist().setBid(bid).queryFirst();
            if (entity == null) {
                return W.error(model, "表单不存在!");
            }
            Map<String, Object> extraParam = tcgInvoiceRegistService.getExtraParam(bid);
            
            List<Map<String, Object>> sonlist = tcgInvoiceRegistService.getItemList(bid);

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCG_InvoiceRegist, sonlist);

            String aa = commonApiService.toEdit(model, entity.getId(), new TcgInvoiceRegist(), MenuId.TCG_InvoiceRegist, extraParam, subGridConfig);
            
            
            if ("test".equals(MyConfig.getActive()) || "pro".equals(MyConfig.getActive())) {
                return "purchase/tcgInvoiceRegist/toFlow";
			} else {
				return aa;
			}
        } catch (Exception e) {
            e.printStackTrace();
            return W.error(model, e.getMessage());
        }
    }
}
